﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RealState.Utils;
using RealState.BLL;
using RealState.BusinessObjects;

namespace RealState.W
{
    public partial class ChangePassword : System.Web.UI.Page
    {
        private UsersBLL userBLL;
        private Users userBO;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Convert.ToString(Session["CustomerID"])))
                Response.Redirect("Default.aspx");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (!Page.IsValid)
                return;
            if (!Converter.MD5Encrypt(txtOldPassword.Text).Equals(Convert.ToString(Session["Password"])))
            {
                ShowMsg("Mật khẩu cũ không đúng");
                return;
            }
            else
            {
                try
                {
                    userBLL = new UsersBLL();
                    userBO = new Users();
                    userBO.Id = Convert.ToInt32(Session["CustomerID"]);
                    userBO.Password = Converter.MD5Encrypt(txtPassword.Text);
                    int result = userBLL.UpdateUsers_2(userBO);
                    if (result > 0)
                    {
                        ShowMsg("Thay đổi thành công");
                    }

                }
                catch (Exception ex)
                {

                    throw;
                }
            }
        }
        public static void ShowMsg(string message)
        {
            string cleanMessage = message.Replace("'", "\'");
            string script = "<script type='text/javascript'>alert('" + cleanMessage + "');</script>";
            Page page = HttpContext.Current.CurrentHandler as Page;
            if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert"))
            {
                page.ClientScript.RegisterClientScriptBlock(typeof(ChangePassword), "alert", script);
            }
        }
    }
}
